#ifndef __VE_CORE_JOB_MANAGER_H__
#define __VE_CORE_JOB_MANAGER_H__

#include "core/threads.h"

#define MAX_WORKER_THREAD_COUNT     32
#define MAX_JOBS_PER_THREAD       1024

#define JOB_PRIORITY_NORMAL       0
#define JOB_PRIORITY_LOW          1
#define JOB_PRIORITY_BACKGROUND   2

#define JOB_THREAD_MAIN         0
#define JOB_THREAD_BACKGROUND  -1
#define JOB_THREAD_WORKER0      1

#define JOB_SPLIT_MODE_POP      0
#define JOB_SPLIT_MODE_DIVIDE   1

typedef struct
{
	VEAtomicInt ref_count;
	int         priority;
} VEJobState;

typedef struct
{
	int begin;
	int end;
} VEJobNumRange;

typedef struct
{
	void *begin;
	void *end;
} VEJobPtrRange;

typedef union
{
	VEJobNumRange num;
	VEJobPtrRange ptr;
} VEJobRange;

typedef struct
{
	void       *context;
	VEJobRange  range;
} VEJobData;

typedef void (*VEJobProcess)( const VEJobData *data, int thread_id );
typedef int  (*VEJobSplit)( VEJobData *old_data, VEJobData *new_data, int mode );

void ve_job_manager_init();
void ve_job_manager_done();

int  ve_worker_thread_count();

void ve_job_start( VEJobState *state, const VEJobData *data, VEJobProcess process, VEJobSplit split, int priority, int thread_id );
void ve_job_wait( VEJobState *state, int thread_id );
int  ve_job_is_ready( VEJobState *state );

#endif /* __VE_CORE_JOB_MANAGER_H__ */
